package com.spic.business.hidden.place.service.impl;

import com.spic.business.hidden.place.domain.HiddenPlace;
import com.spic.business.hidden.place.domain.vo.TreeSelect;
import com.spic.business.hidden.place.mapper.HiddenPlaceMapper;
import com.spic.business.hidden.place.service.IHiddenPlaceService;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 隐患地点Service业务层处理
 *
 * @author yangjie
 * @date 2021-10-15
 */
@Service
public class HiddenPlaceServiceImpl implements IHiddenPlaceService
{
    @Autowired
    private HiddenPlaceMapper hiddenPlaceMapper;

    /**
     * 查询隐患地点
     *
     * @param id 隐患地点主键
     * @return 隐患地点
     */
    @Override
    public HiddenPlace selectHiddenPlaceById(String id)
    {
        return hiddenPlaceMapper.selectHiddenPlaceById(id);
    }

    /**
     * 查询隐患地点列表
     *
     * @param hiddenPlace 隐患地点
     * @return 隐患地点
     */
    @Override
    public List<HiddenPlace> selectHiddenPlaceList(HiddenPlace hiddenPlace)
    {
        return hiddenPlaceMapper.selectHiddenPlaceList(hiddenPlace);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<HiddenPlace> hiddenPlaces)
    {
        List<HiddenPlace> deptTrees = buildPlaceTree(hiddenPlaces);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 新增隐患地点
     *
     * @param hiddenPlace 隐患地点
     * @return 结果
     */
    @Override
    public int insertHiddenPlace(HiddenPlace hiddenPlace)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        hiddenPlace.setId(uuid);
        return hiddenPlaceMapper.insertHiddenPlace(hiddenPlace);
    }

    /**
     * 修改隐患地点
     *
     * @param hiddenPlace 隐患地点
     * @return 结果
     */
    @Override
    public int updateHiddenPlace(HiddenPlace hiddenPlace)
    {
        return hiddenPlaceMapper.updateHiddenPlace(hiddenPlace);
    }

    /**
     * 批量删除隐患地点
     *
     * @param ids 需要删除的隐患地点主键
     * @return 结果
     */
    @Override
    public int deleteHiddenPlaceByIds(String[] ids)
    {
        return hiddenPlaceMapper.deleteHiddenPlaceByIds(ids);
    }

    /**
     * 删除隐患地点信息
     *
     * @param id 隐患地点主键
     * @return 结果
     */
    @Override
    public int deleteHiddenPlaceById(String id)
    {
        return hiddenPlaceMapper.deleteHiddenPlaceById(id);
    }


    @Override
    public List<HiddenPlace> selectPlacebyHiddeType(HiddenPlace hiddenPlace)
    {
        hiddenPlace.setParentId("100");
        return hiddenPlaceMapper.selectPlacebyHiddeType(hiddenPlace);
    }

    @Override
    public List<HiddenPlace> selectgetplaceByParentid(String parentId)
    {
        return hiddenPlaceMapper.selectgetplaceByParentid(parentId);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param hiddenPlaces 部门列表
     * @return 树结构列表
     */
    @Override
    public List<HiddenPlace> buildPlaceTree(List<HiddenPlace> hiddenPlaces)
    {
        List<HiddenPlace> returnList = new ArrayList<HiddenPlace>();
        List<String> tempList = new ArrayList<String>();
        for (HiddenPlace hiddenPlace : hiddenPlaces)
        {
            tempList.add(hiddenPlace.getId());
        }
        for (Iterator<HiddenPlace> iterator = hiddenPlaces.iterator(); iterator.hasNext();)
        {
            HiddenPlace hiddenPlace = (HiddenPlace) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(hiddenPlace.getParentId()))
            {
                recursionFn(hiddenPlaces, hiddenPlace);
                returnList.add(hiddenPlace);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = hiddenPlaces;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<HiddenPlace> list, HiddenPlace t)
    {
        // 得到子节点列表
        List<HiddenPlace> childList = getChildList(list, t);
        t.setChildren(childList);
        for (HiddenPlace tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<HiddenPlace> getChildList(List<HiddenPlace> list, HiddenPlace t)
    {
        List<HiddenPlace> tlist = new ArrayList<HiddenPlace>();
        Iterator<HiddenPlace> it = list.iterator();
        while (it.hasNext())
        {
            HiddenPlace n = (HiddenPlace) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<HiddenPlace> list, HiddenPlace t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
